#include "UP_test.h"
#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "UP_GPIO.h"
#include "UP_USART.h"
#include "UP_ADC.h"
#include "UP_PWM.h"

#include "Gui.h"
#include "bsp_spi_lcd.h"
#include "gui_canvas.h"
#include "pic.h"
#include "System.h"
#include "Task.h"
#include "Image.h"
#include "greedy_snake_app.h"

void SPI_test()
{
    // 核心板
    systick_config(); // 滴答定时器初始化

    // 扩展板
    Lcd_Init(); // spi lcd初始化

    // uint16_t image_w = 222;
    // uint16_t image_h = 60;
    uint16_t image_w = 40;
    uint16_t image_h = 40;

    /* SPI LCD */
    LCD_Fill(0, 0, LCD_W, LCD_H, COLOR_BLUE);                                  // 深蓝色 背景
    LCD_ShowPicture((LCD_W - image_w) / 2, 0, image_w, image_h, gImage_1);     // 显示图片 gImage_reba，gImage_lCKFB
    LCD_DrawRectangle(10, 60, LCD_W - 10, LCD_H - 10, COLOR_WHITE);            // 绘制一个矩形
    LCD_ShowChinese(15, 60 + 5, "开始游戏", COLOR_WHITE, COLOR_BLUE, 12, 0);   // 显示12汉字字符串
    LCD_ShowString(15, 60 + 5 + 17, "123abc", COLOR_WHITE, COLOR_BLUE, 16, 0); // 显示16字符串

    while (1) {
        ;
    }
}

u16 app_CanvasBuff[LCD_W * LCD_H];
Canvas app_Canvas = {app_CanvasBuff};
void App_Init()
{
    app_Canvas.canvasP = app_CanvasBuff;
    Gui_SetCanvas(&app_Canvas, 0, 0, Lcd_GetW(), Lcd_GetH()); // 设置画布
    // Gui_Image(0, 0, &image_Home);                             // 显示图片
    // Gui_CanvasRefsh(0, 0, Lcd_GetW(), Lcd_GetH());            // 更新画布到屏幕
    LCD_BLK_H; // 打开背光
}

#define LIST_X  Lcd_GetW() / 2
#define LIST_Y  120
#define SPACE_Y 30
void SPI_test_canvas()
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);

    nvic_irq_enable(SysTick_IRQn, 0, 0); // Delay延时
    nvic_irq_enable(TIMER2_IRQn, 1, 1);  // System系统计时
    nvic_irq_enable(TIMER3_IRQn, 4, 4);  // Task任务
    // 核心板 ----------------------------------------------------
    systick_config(); // 滴答定时器初始化

    USART_init(         // 串口日志
        USART0,         // USART USARTx(x=0,1,2,5) / UARTx(x=3,4,6,7)
        115200U,        // 波特率
        USART_WL_8BIT,  // 数据位
        USART_STB_1BIT, // 停止位
        USART_PM_NONE   // 校验位
    );

    System_Init(); // 系统计时

    // 扩展板 ----------------------------------------------------
    PWM_init(TIMER1_CH2_PA2_AF1, 1000, 0);
    Lcd_Init(); // spi lcd初始化

    ADC_init(ADC0); // 摇杆
    GPIO_Init(PC1, 5); // 摇杆x
    GPIO_Init(PA1, 5); // 摇杆y
    
    // 游戏 ----------------------------------------------------
    Gui_Init(); // 初始化GUI
    App_Init(); // 初始化APP


    int count              = 0;
    int index              = 0;
    int indexY             = Lcd_GetH();
    PaintColor paintColor0 = PaintColorMake(ColorMake(COLOR_BLUE, 0), ColorMake(0, 100));
    PaintColor paintColor1 = PaintColorMake(ColorMake(COLOR_LIGHTBLUE, 0), ColorMake(0, 160));
    PaintColor paintColor  = paintColor0;
    Color colorBG          = ColorMake(COLOR_GREEN, 0);

    greedy_snake_init();
    Gui_CanvasRefsh(0, 0, Lcd_GetW(), Lcd_GetH());

    while (1) {
        greedy_snake_task();

        TaskMs_EventCycle(10);

        if (TaskMs_EventTime(0)) {
            u32 time1 = SysTime_GetMs();

            // Gui_SetCanvas(&app_Canvas, 0, 0, Lcd_GetW(), Lcd_GetH());

#if IMAGE_ENABLE
            Gui_Image(0, 0, &image_Home);
#endif // DEBUG

            // Gui_Rect(0, 0, Lcd_GetW(), Lcd_GetH(), colorBG);

            // if (indexY > LIST_Y) {
            //     float dy = (indexY - LIST_Y) * 0.15;
            //     indexY -= dy;
            // }
            greedy_snake_run(0);

            u32 time2 = SysTime_GetMs();

            // 将str和count拼接成一个字符串
            // char *str = " 开始游戏 ";
            // char strCount[20];
            // sprintf(strCount, "%s%d", str, count);
            // Gui_StringRelative(strCount, LIST_X, 100 + SPACE_Y * 0, fontSize_16, paintColor, 0.5, 0);
            // count++;

            Gui_CanvasRefsh(0, 0, Lcd_GetW(), Lcd_GetH());

            u32 calc_duration = time2 - time1;
            u32 draw_duration = SysTime_GetMs() - time2;
            printf("calc: %d ms, draw: %d ms\n", calc_duration, draw_duration);
        }
    }
}